package me.zhengjie.modules.system.service.dto

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.annotation.JsonTypeInfo
import com.fasterxml.jackson.annotation.JsonTypeName
import io.swagger.v3.oas.annotations.media.Schema
import me.zhengjie.common.base.BaseDTO
import me.zhengjie.common.utils.JacksonUtil
import java.util.*

/**
 * @author Kuki Wu
 * @date 2018-12-17
 */
@Schema(description = "系统菜单dto类")
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
@JsonTypeName("MenuDTO")
data class MenuDTO(
    var id: Long? = null,
    @get:JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
    @get:JsonInclude(JsonInclude.Include.NON_EMPTY)
    var children: List<MenuDTO>? = null,
    var type: Int? = null,
    var permission: String? = null,
    var title: String? = null,
    var menuSort: Long? = null,
    var path: String? = null,
    var component: String? = null,
    var pid: Long? = null,
    var subCount: Int? = null,
    var iFrame: Boolean? = null,
    var cache: Boolean? = null,
    var hidden: Boolean? = null,
    var componentName: String? = null,
    var icon: String? = null,

): BaseDTO(){
    annotation class Update

    companion object {
        fun fromJson(json: String): MenuDTO? {
            val obj=JacksonUtil.parseObject(json, MenuDTO::class.java)!!
            return when(obj){
                is MenuDTO -> obj
                else -> null
            }
        }
    }

    fun getHasChildren(): Boolean {
        return subCount!! > 0
    }

    fun getLeaf(): Boolean {
        return subCount!! <= 0
    }

    fun getLabel(): String {
        return title!!
    }

    override fun equals(o: Any?): Boolean {
        if (this === o) {
            return true
        }
        if (o == null || javaClass != o.javaClass) {
            return false
        }
        val menuDto: MenuDTO =
            o as MenuDTO
        return id == menuDto.id
    }

    override fun hashCode(): Int {
        return Objects.hash(id)
    }

    fun copy(source: MenuDTO?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}